from functools import lru_cache
from inspect import signature
from typing import Any, Dict, Iterable, List, Optional, Tuple, Union
from uuid import UUID


ROUTER_CACHE_SIZE = 1024
ALLOWED_LABELS = ("__file_uri__",)


# TODO: 所有的路由处理只用这一个类处理
# class Router(BaseRouter):
#     ...


class Router:
    routes = {
        '/': 'Hello World!',
        '/hello': '你好，世界！',
    }

    @staticmethod
    def get_response(path):
        if path in Router.routes:
            return Router.routes[path]
        else:
            return '404 找不到页面'


import asyncio
from http.server import HTTPServer, BaseHTTPRequestHandler


class MyHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/':
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            self.wfile.write(b'Hello, World!')
        elif self.path == '/about':
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            self.wfile.write(b'About Page')


async def middleware(request, handler):
    print('Before handling request')
    response = await handler(request)
    print('After handling request')
    return response


class MyHTTPServer(HTTPServer):
    def __init__(self, server_address, RequestHandlerClass, middleware):
        super().__init__(server_address, RequestHandlerClass)
        self.middleware = middleware

    def handle_request(self):
        self.request_handler = self.middleware(self.request_handler)
        super().handle_request()


async def run_server():
    server_address = ('', 8888)
    httpd = MyHTTPServer(server_address, MyHandler, middleware)
    httpd.serve_forever()

loop = asyncio.get_event_loop()
loop.run_until_complete(run_server())
